package test.algorithm.astar;

import java.awt.Point;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RectGraph extends GridGraph {

	private Point[] moveableDirections = MoveableDirections.ROOK;
   
    public RectGraph(boolean isDynamicEdges, int rows, int cols) {
        super(isDynamicEdges, rows, cols);
    }
   
    public int[][] initGraph(int width, int height) {
    	
    	int [][] map = new int[width][height];
    	int idx = 1;
    	for (int i=0; i<width; i++) {
    		for (int k=0; k<height; k++) {
    			map[i][k] = idx;
    			idx++;
    		}
    	}
	    return map;
    }
   
	public Map<Integer, Map<String, Integer>> initPathToFind() {
		Map<Integer, Map<String, Integer>> pathToFind = new HashMap<Integer, Map<String, Integer>>();
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("WIDTH", 2);
		map.put("HEIGHT", 2);
		pathToFind.put(0, map);
		
		map = new HashMap<String, Integer>();
		map.put("WIDTH", 1);
		map.put("HEIGHT", 4);
		pathToFind.put(1, map);
		
		map = new HashMap<String, Integer>();
		map.put("WIDTH", 3);
		map.put("HEIGHT", 3);
		pathToFind.put(2, map);
		
		map = new HashMap<String, Integer>();
		map.put("WIDTH", 4);
		map.put("HEIGHT", 1);
		pathToFind.put(3, map);
		
		map = new HashMap<String, Integer>();
		map.put("WIDTH", 5);
		map.put("HEIGHT", 3);
		pathToFind.put(4, map);
		
		map = new HashMap<String, Integer>();
		map.put("WIDTH", 5);
		map.put("HEIGHT", 5);
		pathToFind.put(5, map);
		
		return pathToFind;
	}
   
	public Point[] getMoveableDirections() {
		return moveableDirections;
	}
   
	public void setMoveableDirections(Point[] moveableDirections) {
		this.moveableDirections = moveableDirections;
	}
   
	public int getMovementCost(Point p) {
	if (p.x == 0 || p.y == 0) return 10;
		return 14;
	}
   
	public void updateEdges(PathNode pathNode) {
		GridNode node = (GridNode) pathNode;
		node.getPathEdges().clear();
		if (node.getTerrain() == Terrain.BLOCKED) return;
		for (Point dir : moveableDirections) {
			Point adjPnt = new Point(node.getRow() + dir.x, node.getCol() + dir.y);
			if (isInBounds(adjPnt.x, adjPnt.y)) {
				GridNode gnode = getGridNode(adjPnt.x, adjPnt.y);
				if (gnode.getTerrain() != Terrain.BLOCKED) {
					node.getPathEdges().add(new PathEdge(gnode, getMovementCost(dir)));
				}
			}
		}
	}
   
	public void print(List<PathNode> path) {
		for (int r = 0; r < rows; r++) {
			for (int c = 0; c < cols; c++) {
	            if (nodes[r][c].getTerrain() == Terrain.BLOCKED) {
	               System.out.print("#");
	            } else if (path.contains(nodes[r][c])) {
	               System.out.print("*");
	            } else {
	               System.out.print(".");
	            }
	            System.out.print(" ");
			}
			System.out.println();
		}
	}
}
